#!/usr/bin/perl

@lines = <STDIN>;

%hash = ();

foreach $line (@lines)
{
	($pos,$string) = split(/\s+/,$line);
	@array = split(/___/,$string);
	$root_str = "";
	$pos_str = "";
	foreach $element (@array)
	{
		$element =~ /(.*)\[(.*)\]/;
		$pos_str = $pos_str. $1 ." ";
		@words = split(/---/,$2);
		foreach $word (@words)
		{
			$word =~ /(.*)\+(.*)\+(.*)\+(.*)/;
			$root = $1;
			$root_str = $root_str.$root." ";
		}
	}
	chop($root_str);
	chop($pos_str);
	$root_str = $pos_str;
#	print $pos ."  ". $pos_str."\n";
	if($hash{ $pos } eq "")
	{
		$hash{ $pos } = $root_str;
	}
	else
	{
		$hash{ $pos } .= "___".$root_str;
	}
}

mkdir($ARGV[0], 0777) || print $!;
while ( ($k,$v) = each %hash ) 
{
	$file = "$ARGV[0]/".$k;
	open(FILE,">$file");
	(@strings) = split(/___/,$v);


	foreach $string (@strings)
	{
		print FILE $string."\n";
	}
	close(FILE);
	$command = "ngram-count -text ".$file." -order 5 -write temp.out";
	system($command);
	$lm = $file.".lm";
	$command = "ngram-count -read temp.out -lm ".$lm;
	system($command);


		
}
